home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / rules / prs2 / prs2retrieve.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  4.4 KB  |  166 lines

  1. /*===================================================================
  2.  *
  3.  * FILE:
  4.  *   prs2retrieve.c
  5.  *
  6.  * IDENTIFICATION:
  7.  *   $Header: /private/postgres/src/rules/prs2/RCS/prs2retrieve.c,v 1.16 1991/05/09 18:10:16 sp Exp $
  8.  *
  9.  * DESCRIPTION:
  10.  *   The main routine is prs2Retrieve() which is called whenever
  11.  *   a retrieve operation is performed by the executor.
  12.  *
  13.  *
  14.  *===================================================================
  15.  */
  16.  
  17. #include "tmp/c.h"
  18. #include "utils/log.h"
  19. #include "rules/prs2.h"
  20. #include "nodes/execnodes.h"    /* which includes access/rulescan.h */
  21.  
  22. /*-------------------------------------------------------------------
  23.  *
  24.  * prs2Retrieve
  25.  */
  26. Prs2Status
  27. prs2Retrieve(prs2EStateInfo, relationRuleInfo,
  28.         explainRelation, tuple, buffer, attributeArray,
  29.         numberOfAttributes, relation, returnedTupleP, returnedBufferP)
  30. Prs2EStateInfo prs2EStateInfo;
  31. RelationRuleInfo relationRuleInfo;
  32. Relation explainRelation;
  33. HeapTuple tuple;
  34. Buffer buffer;
  35. AttributeNumberPtr attributeArray;
  36. int numberOfAttributes;
  37. Relation relation;
  38. HeapTuple *returnedTupleP;
  39. Buffer *returnedBufferP;
  40. {
  41.     RuleLock locks, locksInTuple, locksInRelation;
  42.     int i;
  43.     AttributeValues attrValues;
  44.     int newTupleMade;
  45.     Name relName;
  46.     bool insteadRuleFound;
  47.     bool insteadRuleFoundThisTime;
  48.  
  49.  
  50.     /*
  51.      * Find the locks of the tuple.
  52.      *
  53.      * There are 2 places to look for locks. The "tuple-level"
  54.      * locks are stored in the tuple, and the "relation-level"
  55.      * locks are stored in the PG_RELATION relation, but we
  56.      * have also copied them (for efficiency) in 'relationRuleInfo'
  57.      * when initializing the plan.
  58.      *
  59.      * BTW, ignore the tuple locks if the appropriate flag
  60.      * is set in 'relationRuleInfo'. This happens when we scan
  61.      * "pg_class".
  62.      */
  63.     locksInRelation = relationRuleInfo->relationLocks;
  64.     if (relationRuleInfo->ignoreTupleLocks) {
  65.     locksInTuple = prs2MakeLocks();
  66.     } else {
  67.     locksInTuple = prs2GetLocksFromTuple(tuple, buffer);
  68.     }
  69.     locks = prs2LockUnion(locksInRelation, locksInTuple);
  70.     prs2FreeLocks(locksInTuple);
  71.  
  72.     /*
  73.      * If there are no rules, then return immediatelly...
  74.      */
  75.     if (prs2GetNumberOfLocks(locks)==0) {
  76.     prs2FreeLocks(locks);
  77.     return(PRS2_STATUS_TUPLE_UNCHANGED);
  78.     }
  79.  
  80.     /*
  81.      * now extract from the tuple the array of the attribute values.
  82.      */
  83.     attrValues = attributeValuesCreate(tuple, buffer, relation);
  84.  
  85.     /*
  86.      * First activate all the 'late evaluation' rules, i.e.
  87.      * all the rules that try to update the fields to be retrieved.
  88.      */
  89.     for(i=0; i<numberOfAttributes; i++) {
  90.     prs2ActivateBackwardChainingRules(
  91.             prs2EStateInfo,
  92.             explainRelation,
  93.             relation,
  94.             attributeArray[i],
  95.             PRS2_OLD_TUPLE,
  96.             tuple->t_oid,
  97.             attrValues,
  98.             locks,
  99.             LockTypeRetrieveWrite,
  100.             InvalidObjectId,
  101.             InvalidAttributeValues,
  102.             InvalidRuleLock,
  103.             LockTypeInvalid,
  104.             attributeArray,
  105.             numberOfAttributes);
  106.     }
  107.  
  108.     /*
  109.      * Then, activate all the 'forward chaining rules'
  110.      */
  111.     insteadRuleFound = false;
  112.     for(i=0; i<numberOfAttributes; i++) {
  113.     prs2ActivateForwardChainingRules(
  114.             prs2EStateInfo,
  115.             explainRelation,
  116.             relation,
  117.             attributeArray[i],
  118.             LockTypeRetrieveAction,
  119.             PRS2_OLD_TUPLE,
  120.             tuple->t_oid,
  121.             attrValues,
  122.             locks,
  123.             LockTypeRetrieveWrite,
  124.             InvalidObjectId,
  125.             InvalidAttributeValues,
  126.             InvalidRuleLock,
  127.             LockTypeInvalid,
  128.             &insteadRuleFoundThisTime,
  129.             attributeArray,
  130.             numberOfAttributes);
  131.     if (insteadRuleFoundThisTime) {
  132.         insteadRuleFound = true;
  133.     }
  134.     }
  135.  
  136.     /*
  137.      * Now all the correct values are in the attrValues array.
  138.      * Create a new tuple with the correct values.
  139.      */
  140.     newTupleMade = attributeValuesMakeNewTuple(
  141.                 tuple, buffer,
  142.                 attrValues, locks, LockTypeRetrieveWrite,
  143.                 relation, returnedTupleP);
  144.  
  145.     prs2FreeLocks(locks);
  146.  
  147.     attributeValuesFree(attrValues, relation);
  148.  
  149.     if (insteadRuleFound) {
  150.     return(PRS2_STATUS_INSTEAD);
  151.     }
  152.  
  153.     if (newTupleMade) {
  154.     *returnedBufferP = InvalidBuffer;
  155.     /*
  156.      * The new tuple must have the same locks as the old
  157.      * tuple (in case we are retrieving the "lock" attribute.
  158.      */
  159.     locksInTuple = prs2GetLocksFromTuple(tuple, buffer);
  160.     HeapTupleSetRuleLock(*returnedTupleP, InvalidBuffer, locksInTuple);
  161.     return(PRS2_STATUS_TUPLE_CHANGED);
  162.     } else {
  163.     return(PRS2_STATUS_TUPLE_UNCHANGED);
  164.     }
  165. }
  166.